#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
    int minIncrements(int n, vector<int> &cost)
    {
        return balance(1, cost).first;
    }

    // ops , val
    pair<int, int> balance(int node, vector<int> &cost)
    {
        if (node * 2 < cost.size())
        {
            auto [opsl, vall] = balance(node * 2, cost);
            auto [opsr, valr] = balance(node * 2 + 1, cost);
            return {opsl + opsr + abs(vall - valr), max(vall, valr) + cost[node - 1]};
        }
        else
            return {0, cost[node - 1]};
    }
};
int main()
{
    cout << "Code2673" << endl;
    Solution s;
    vector<int> cost = {1, 5, 2, 2, 3, 3, 1};
    cout << s.minIncrements(cost.size(), cost) << endl;
    return 0;
}